<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Db;
use app\index\model\Personals;
use app\index\model\Users;
use app\index\model\Attentions;
class Attention extends Common
{  
	//添加关注
	public function attention_add(){
		$uid=input('uid','');//获取用户id
		$nearby_id=input('nearby_id','');//关注的用户id
		if(!empty($uid) && !empty($nearby_id)){
           $rs=Users::selOne($uid);//查询我是否存在
          $rd2=Db::name('user_info')->where(['uid'=>$uid,'member_type'=>'2'])->find();
          if(!empty($rd2)){
            return json(['state'=>'207','msg'=>'领队无权关注']);
          }
           $opinion=Users::selOne($nearby_id);//查询Ta是否存在
           if($rs){
            if($rs['focusNum']==0){
              return json(['state'=>'208','msg'=>'关注次数已用完']);
            }
            if(empty($opinion)){
              return json(['state'=>'205','msg'=>'Ta已不存在']);
            }
            //我关注Ta
             $rd=Attentions::attentionSel($uid,$nearby_id);
             if(empty($rd)){
                $re=Attentions::fansSel($nearby_id,$uid);//判断我的粉丝
                //print_r($re);die;
                 Db::startTrans(); 
                if($re){
                      try{ 
                    $data=['follow_uid'=>$nearby_id,'update_time'=>date("Y-m-d H:i:s"),'status'=>'1'];//我的
                    $sdata=['fans_uid'=>$uid,'update_time'=>date("Y-m-d H:i:s"),'status'=>'1'];//Ta的
                    //修改我的关注好友id
                   $resel=Db::name('user_relation')->where(['uid'=>$uid,'fans_uid'=>$nearby_id,'id'=>$re['id']])->update($data);
                   if(!empty($resel)){
                     $focus=1;//相互关注状态;
                   }
                    //修改Ta的粉丝好友id
                    $rk=Attentions::neaFans($uid,$nearby_id);
                    if(empty($rk)){
                      //我是Ta的关注者
                       Db::name('user_relation')->where(['uid'=>$nearby_id,'follow_uid'=>$uid])->update($sdata);
                    }
                   Db::name('user_info')->where('uid',$uid)->setInc('attention');//我的关注数量增加1
                   Db::name('user_info')->where('uid',$nearby_id)->setInc('fans');//Ta的粉丝数量增加1
                   Db::name('user_info')->where('uid',$uid)->setDec('focusNum');//减少我的关注次数1
                   //用于消息通知
                   $sdata1=['nickname'=>$opinion['nickname'],'userPhoto'=>$opinion['userPhoto'],'genreId'=>'1','create_time'=>time()];
                    $message=Db::name('user_message')->where(['uid'=>$uid,'peerId'=>$nearby_id,'genreId'=>'1'])->update($sdata1);
                   if(empty($message)){
                    $sdata1['uid']=$uid; $sdata1['peerId']=$nearby_id;
                      Db::name('user_message')->insert($sdata1);
                   }
                  // 提交事务
                  Db::commit(); 
                   return json(['state'=>'200','msg'=>'关注成功','result'=>$focus]);
                  }catch(\Exception $e){             
                  // 回滚事务 
                  Db::rollback(); 
                   return json(['state'=>'201','msg'=>'关注失败']);
                  } 
                }else{ 
                try{ 
                    $data=['uid'=>$uid,'follow_uid'=>$nearby_id,'create_time'=>date("Y-m-d H:i:s")];//我的
                    $sdata=['uid'=>$nearby_id,'fans_uid'=>$uid,'create_time'=>date("Y-m-d H:i:s")];//Ta的
                   $resel=Db::name('user_relation')->insert($data);//添加我的关注好友id
                   if($resel){
                    $focus=2;//已关注状态
                    }
                    //查询Ta的粉丝
                    $rk=Attentions::neaFans($uid,$nearby_id);
                    if(empty($rk)){
                    	Db::name('user_relation')->insert($sdata);//添加Ta的粉丝好友id
                      
                    }else{
                    	$mdata=['follow_uid'=>'0','uid'=>$nearby_id,'fans_uid'=>$uid,'create_time'=>date("Y-m-d H:i:s")];
                    	 Db::name('user_relation')
                    	 ->where(['uid'=>$nearby_id,'fans_uid'=>$uid,'id'=>$rk['id']])
                    	 ->update($mdata);
                    }
                   //用于消息通知
                   $sdata1=['nickname'=>$opinion['nickname'],'userPhoto'=>$opinion['userPhoto'],'genreId'=>'1','create_time'=>time()];
                   $message=Db::name('user_message')->where(['uid'=>$uid,'peerId'=>$nearby_id,'genreId'=>'1'])->update($sdata1);
                   if(empty($message)){
                     $sdata1['uid']=$uid; $sdata1['peerId']=$nearby_id;
                      Db::name('user_message')->insert($sdata1);
                   }
                   Db::name('user_info')->where('uid',$uid)->setInc('attention');//我的关注数量增加1
                   Db::name('user_info')->where('uid',$nearby_id)->setInc('fans');//Ta的粉丝数量增加1
                   Db::name('user_info')->where('uid',$uid)->setDec('focusNum');//减少我的关注次数1
                  // 提交事务
                  Db::commit(); 
                   return json(['state'=>'200','msg'=>'关注成功','result'=>$focus]);
                  }catch(\Exception $e){             
                  // 回滚事务 
                  Db::rollback(); 
                   return json(['state'=>'201','msg'=>'关注失败']);
                  }
                }
             }else{
               return json(['state'=>'202','msg'=>'您已经关注过啦']);
             }
           }else{
             return json(['state'=>'203','msg'=>'用户不存在']);
           }  
		}else{
			return json(['state'=>'204','msg'=>'参数不足']);
		}
	}
  //取消关注
  public function  cancelSel(){
    $uid=input('uid','');//获取用户id
    $nearby_id=input('nearby_id','');//关注的用户id 
    if(!empty($uid) && !empty($nearby_id)){
           $rs=Users::selOne($uid);
           if($rs){
            //判断我有没有关注Ta
             $rd=Attentions::attentionSel($uid,$nearby_id);
             if(empty($rd)){
                return json(['state'=>'203','msg'=>'你还没有关注过Ta']);
              }
               Db::startTrans(); 
                try{ 
                 $data=['follow_uid'=>'','status'=>''];//我的
                    $sdata=['uid'=>$nearby_id,'fans_uid'=>'','status'=>''];//Ta的
                    //取消我的关注好友id
                    $re=Db::name('user_relation')->where(['uid'=>$uid,'follow_uid'=>$nearby_id,'fans_uid'=>$nearby_id])->update($data);
                    if(empty($re)){
                       Db::name('user_relation')->where(['uid'=>$uid,'follow_uid'=>$nearby_id])->delete();
                    }
                    //取消Ta的关注好友
                    $arrOne=Db::name('user_relation')->where(['uid'=>$nearby_id,'fans_uid'=>$uid,'follow_uid'=>$uid])->update($sdata);
                    if(empty($arrOne)){
                      Db::name('user_relation')->where(['uid'=>$nearby_id,'fans_uid'=>$uid])->delete();//
                    }
                    //判断我的关注量
                   $rb=Db::name('user_info')->where(['uid'=>$uid])->field('attention')->find();
                   if($rb['attention']<=0){
                    $data2=['attention'=>'0'];
                     Db::name('user_info')->where('uid',$uid)->update($data2);//我的关注数量减少1
                   }else{
                     Db::name('user_info')->where('uid',$uid)->setDec('attention');//我的关注数量减少1
                   }
                   //判断Ta的粉丝量
                   $rk=Db::name('user_info')->where('uid',$nearby_id)->field('fans')->find();
                   if($rk['fans']<=0){
                      $data2=['fans'=>'0'];
                     Db::name('user_info')->where('uid',$uid)->update($data2);//我的关注数量减少1
                    }else{
                        Db::name('user_info')->where('uid',$nearby_id)->setDec('fans');//Ta的粉丝数量减少1
                    }
                    // 提交事务
                  Db::commit(); 
                   return json(['state'=>'200','msg'=>'取消关注成功']);
                 }catch(\Exception $e){        
                  // 回滚事务 
                  Db::rollback();
                   return json(['state'=>'201','msg'=>'取消关注失败再试试']);
                  }
             }else{
               return json(['state'=>'202','msg'=>'用户已不存在']);
            }
  }else{
     return json(['state'=>'204','msg'=>'参数缺失']);
  }
}
  //添加领队关注
  public   function captain_add(){
    $uid=input('uid','');//关注的领队id
    $captain_id=input('captain_id','');//关注的领队id
    if(!empty($uid) && !empty($captain_id)){
           $rs=Users::selOne($uid);
           if($rs){
             $rd=Attentions:: captainSel($uid,$captain_id);
             if(!empty($rd)){
                return json(['state'=>'202','msg'=>'您已经关注过啦']);
              }else{
                // 启动事务
                Db::startTrans();
                $data=['uid'=>$uid,'captain_id'=>$captain_id,'create_time'=>date("Y-m-d H:i:s")];
                try{
                  Db::name('user_captain')->insert($data);
                  //增加领队粉丝人数
                  Db::name('user_info')->where(['uid'=>$captain_id])->setInc('fans');
                  // 提交事务
                  Db::commit();  
                 
                  return   json(['state'=>'200','msg'=>'关注成功']);
                } catch (\Exception $e) {
                  // 回滚事务
                  Db::rollback();
                   return  json(['state'=>'201','msg'=>'关注失败']);
                } 
                 }
            }else{
               return json(['state'=>'203','msg'=>'用户已不存在']);
            }     
        }else{
          return json(['state'=>'204','msg'=>'参数不足']);
        }          
  }
  //取消领队关注
  public function captain_del(){
     $uid=input('uid','');//获取用户id
    $captain_id=input('captain_id','');//关注的领队id 
    if(!empty($uid) && !empty($captain_id)){
       $rs=Users::selOne($uid);
       if($rs){
         $rd=Db::name('user_captain')->where(['uid'=>$uid,'captain_id'=>$captain_id])->find();
         if(empty($rd)){
            return json(['state'=>'204','msg'=>'你还没有关注过Ta']);
          }
         $re=Db::name('user_captain')->where(['uid'=>$uid,'captain_id'=>$captain_id])->delete();
         if($re){
          return json(['state'=>'200','msg'=>'取消关注成功']);
         }else{
          return json(['state'=>'201','msg'=>'取消关注失败再试试']);
         }
       }else{
        return json(['state'=>'202','msg'=>'用户已不存在']);
       }
    }else{
      return json(['state'=>'203','msg'=>'参数不足']);
    }      
  }
  //好友聊天的判断
  public function chatDecide(){
     $uid=input('uid','');//用户uid
     $nearby_id=input('nearby_id','');//好友的id
    if(!empty($uid) && !empty($nearby_id)){
           $rs=Users::selOne($uid);
           if($rs){
             $rd=Attentions:: attentionSel($uid,$nearby_id);
             $re=Attentions:: fansSel($nearby_id,$uid);
             if(!empty($rd) || !empty($re)){
                return json(['state'=>'200','msg'=>'已关注,可以聊天啦']);
              }else{
                return json(['state'=>'201','msg'=>'未关注,请先关注']);
                 }
            }else{
               return json(['state'=>'202','msg'=>'用户已不存在']);
            }     
        }else{
          return json(['state'=>'203','msg'=>'参数不足']);
        }       
  }
}
